# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, Http404
from django.contrib import auth
from django.core.mail import send_mail
from intraorg.mock.models import Announcement
from django.core.exceptions import ObjectDoesNotExist
from intraorg.projects.models import *
from intraorg.members.models import *
from intraorg.contacts.models import *
from intraorg.mock.models import *
from django.db.models import Q

def custom_404_view(request):      
    return render_to_response('mock/custom_404.html', {
        'session':request.session,})

def logout(request):
    auth.logout(request)
    return render_to_response('mock/logout.html', { })

def login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = auth.authenticate(username=username, password=password)
        if user is not None and user.is_active:
            # Correct password, and the user is marked "active"
            auth.login(request, user)
            # Redirect to a success page.
            if request.POST['next']:
                return HttpResponseRedirect(request.POST['next'])
            else:
                return HttpResponseRedirect("/")
        else:
            # Pokaż stronę błędu
            return HttpResponseRedirect("/accounts/invalid/")
    else:
        try: 
            next = request.GET['next']
        except: 
            next = None
        return render_to_response('mock/login.html', {'next':next, 'session':request.session})

def login_invalid(request):
    return render_to_response('mock/login_invalid.html', {'session':request.session})

@login_required
def announcements(request, id=None):
    if not id:
        try:
            announcement = Announcement.objects.latest('pub_date')
        except ObjectDoesNotExist:
            announcement = None
    else:
        try:
            announcement = Announcement.objects.get(id=id)
        except ObjectDoesNotExist:
            announcement = None
    if Announcement.objects.count() > 1:
        prev = True
    else:
        prev = False 
    return render_to_response('mock/announcement.html', \
        {'obj':announcement, 'prev':prev, })
def previous_announcements(request):
    try:
        list = Announcement.objects.all().order_by('-pub_date')
    except ObjectDoesNotExist:
        list = None
    return render_to_response('mock/announcement.html', \
        {'list':list, })

@login_required
def search(request):
    results = []
    data = ''
    if request.method == 'POST':
        if 'search' in request.POST:
            data = request.POST['search']
            try:
                query = Project.objects.order_by('id').filter(Q(name__icontains=data)|
                Q(owner__name__icontains=data)|
                Q(status__name__icontains=data)|
                Q(webpage__icontains=data)|
                Q(docspage__icontains=data)|
                Q(about__icontains=data))
                if len(query) > 0:
                    results.append(('projekty', query))
            except:
                pass
            try:
                query = ProjectTeam.objects.order_by('id').filter(Q(responsibilities__icontains=data))
                if len(query) > 0:
                    results.append(('ekipy projektowe', query))
            except:
                pass
            #-------------------------------------------------------------- try:
                # query = ProjectIssue.objects.order_by('id').filter(Q(name__icontains=data)|
                #-------------------------------------- Q(text__icontains=data))
                #-------------------------------------------- if len(query) > 0:
                    #-------------- results.append(('kwestie projektów', query))
            #----------------------------------------------------------- except:
                #---------------------------------------------------------- pass
            try:
                query = JobRequest.objects.order_by('id').filter(Q(name__icontains=data)|
                Q(text__icontains=data)|Q(user__icontains=data))
                if len(query) > 0:
                    results.append(('praca poszukiwana', query))
            except:
                pass
            try:
                query = JobOffert.objects.order_by('id').filter(Q(name__icontains=data)|
                Q(text__icontains=data)|Q(project__icontains=data))
                if len(query) > 0:
                    results.append(('oferty pracy', query))
            except:
                pass
            try:
                query = Member.objects.order_by('id').filter(Q(name__icontains=data)|
                Q(surname__icontains=data)|
                Q(membership__name__icontains=data)|
                Q(nickname__icontains=data)|
                Q(email__icontains=data)|
                Q(jabber__icontains=data)|
                Q(gg__icontains=data)|
                Q(skype__icontains=data)|
                Q(about__icontains=data)|
                Q(interests__icontains=data))
                if len(query) > 0:
                    results.append(('członkowie', query))
            except:
                pass
            try:
                query = Group.objects.order_by('id').filter(Q(name__icontains=data)|
                Q(shortname__icontains=data)|
                Q(leader__name__icontains=data)|
                Q(about__icontains=data))
                if len(query) > 0:
                    results.append(('grupy', query))
            except:
                pass
            try:
                query = Contact.objects.order_by('id').filter(Q(name__icontains=data)|
                Q(description__icontains=data))
                if len(query) > 0:
                    results.append(('kontakty', query))
            except:
                pass
            try:
                query = Announcement.objects.order_by('id').filter(Q(title__icontains=data)|
                Q(text__icontains=data))
                if len(query) > 0:
                    results.append(('komunikaty', query))
            except:
                pass

    return render_to_response('mock/search.html',{
        'list':results, 
        'search':data})
    
